package com.aididi.encrypt.base64;


import org.apache.tomcat.util.codec.binary.Base64;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
 * @Auther: xuzexiang
 * @Date: 2018-11-11 0011 23:22
 * @Description:    Base64  单向加密算法
 */
public class BaseEncrypt {
    private static String code = "abc123";
    public static void main(String[] args) throws Exception {
        jdkEncrypt();
        CommonsEncrypt();
    }

    /**
     * 基于jdk的 base64算法
     * @throws UnsupportedEncodingException
     */
    public static void jdkEncrypt() throws IOException {
        System.out.println(3<<3);
        //方法加密
        BASE64Encoder encoder = new BASE64Encoder();
        String encode = encoder.encode(code.getBytes());
        System.out.println("基于jdk的加密"+encode);
        BASE64Decoder decoder = new BASE64Decoder();
        System.out.println("基于jdk的解密"+new String(decoder.decodeBuffer(encode)));
    }

    /**
     *  基于Apache Commons的 base64算法
     * @throws UnsupportedEncodingException
     */
    public static void CommonsEncrypt() throws UnsupportedEncodingException {
        //方法加密
        byte [] base64Encode =  Base64.encodeBase64(code.getBytes());
        System.out.println("基于Apache Commons的加密"+new String(base64Encode));
        //方法解密
        byte [] decode =  Base64.decodeBase64(base64Encode);
        String base64Decode = new String(decode);
        System.out.println("基于Apache Commons的解密"+base64Decode);
    }

    /**
     *  基于BC的 base64算法
     * @throws UnsupportedEncodingException
     */
    public static void BCEncrypt() throws UnsupportedEncodingException {
        //方法加密

    }


}
